<?php
/**
 * 关联产品模型层
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Model
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: RelationGoodsModel.class.php 7554 2011-06-01 03:20:46Z ouyangxiong $
 * 
 */
class RelationGoodsModel extends CommonModel {
	
	/**
	 * 取得关联产品列表
	 *
	 * @param string $type
	 * @param int $cat_id
	 */
	function get($type='index-tejia', $pid=0){
		$list = $this->table($this->tablePrefix.'relation_goods AS s')->field('s.id,s.goods_id,s.title,s.link,s.maidian,s.src,s.sort_order,g.goods_name,g.goods_maidian,g.click_count,g.goods_thumb,g.shop_price,g.market_price')
				->join($this->tablePrefix.'goods AS g ON g.goods_id=s.goods_id')
				->where("g.is_delete=0 AND g.is_on_sale=1 AND s.type='$type' AND s.pid=$pid")
				->order('s.sort_order ASC')
				->findAll();
		foreach($list as $key=>$goods){
			$list[$key]['url'] = empty($goods['link']) ? U('mall://Product/index', array('id'=>$goods['goods_id'])) : $goods['link'];
			$list[$key]['thumb'] = empty($goods['src']) ? $this->getImagePath($goods['goods_thumb']) : $goods['src'];
			$list[$key]['name'] = empty($goods['title']) ? $goods['goods_name'] : $goods['title'];
			$list[$key]['title'] = $list[$key]['name'];
			$list[$key]['price'] = D('Goods')->realPrice($goods['goods_id']);
			$list[$key]['format_price'] = $this->formatPrice($list[$key]['price']);
			$list[$key]['format_market_price'] = $this->formatPrice($goods['market_price']);
			$list[$key]['maidian'] = empty($goods['maidian']) ? $goods['goods_maidian'] : $goods['maidian'];
		}
		return $list;
	}
	function get_limit($type='index-tejia',$limit=5, $pid=0){
		$list = $this->table($this->tablePrefix.'relation_goods AS s')->field('s.id,s.goods_id,s.title,s.link,s.maidian,s.src,s.sort_order,g.goods_name,g.goods_maidian,g.click_count,g.goods_thumb,g.shop_price,g.market_price')
				->join($this->tablePrefix.'goods AS g ON g.goods_id=s.goods_id')
				->where("g.is_delete=0 AND g.is_on_sale=1 AND s.type='$type' AND s.pid=$pid")
				->order('s.sort_order ASC')
				->limit($limit)->select();
		foreach($list as $key=>$goods){
			$list[$key]['url'] = empty($goods['link']) ? U('mall://Product/index', array('id'=>$goods['goods_id'])) : $goods['link'];
			$list[$key]['thumb'] = empty($goods['src']) ? $this->getImagePath($goods['goods_thumb']) : $goods['src'];
			$list[$key]['name'] = empty($goods['title']) ? $goods['goods_name'] : $goods['title'];
			$list[$key]['title'] = $list[$key]['name'];
			$list[$key]['price'] = D('Goods')->realPrice($goods['goods_id']);
			$list[$key]['format_price'] = $this->formatPrice($list[$key]['price']);
			$list[$key]['format_market_price'] = $this->formatPrice($goods['market_price']);
			$list[$key]['maidian'] = empty($goods['maidian']) ? $goods['goods_maidian'] : $goods['maidian'];
		}
		return $list;
	}
	
	/**
	 * 关联商品详情
	 *
	 * @param int $id
	 * @return array
	 */
	function getDetail($id=0){
		if(empty($id)) return array();
		$detail = $this->table($this->tablePrefix.'relation_goods AS s')->field('s.*,g.goods_name,g.goods_maidian,g.goods_thumb,g.shop_price')
				->join($this->tablePrefix.'goods AS g ON g.goods_id=s.goods_id')
				->where('g.is_delete=0 AND g.is_on_sale=1 AND s.id='.$id)
				->order('s.sort_order ASC')
				->find();
		if(empty($detail)){
			return array();
		}
		$detail['thumb'] = empty($detail['src']) ? $detail['goods_thumb'] : $detail['src'];
		$detail['price'] = D('Goods')->realPrice($detail['goods_id']);
		return $detail;
	}
}
?>